﻿using PlaceOrder_Client.ViewModels;
using PlaceOrder_Shared.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PlaceOrder_Client.Views
{
    /// <summary>
    /// ConfirmationDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmationDialog : Window
    {
        public bool IsConfirmed { get; private set; }

        public ConfirmationDialog(UserTrade userTrade)
        {
            InitializeComponent();

            // Set the message text
            this.tscode.Text = userTrade.TsCode;

            var receivers = OrderListViewModel.ChatReceivers;

            this.broker.Text = userTrade.Target;
            //this.speed.Text = userTrade.DeliveryDate.ToString();

            var deliverySpeed = userTrade.DeliverySpeedFormat.ToString();
            var parts = deliverySpeed.Split(' '); // 假设格式是 "07/19 +0"

            speed.Inlines.Clear(); // 清空现有内容
            //speed.Inlines.Add(new Run("清算速度 "));
            speed.Inlines.Add(new Run(parts[0] + " ")); // 添加日期部分
            speed.Inlines.Add(new Run(parts[1])
            {
                Foreground = System.Windows.Media.Brushes.CadetBlue,
                FontWeight = FontWeights.Bold
            }); // 

            this.price.Text = userTrade.Price.ToString("F4");
            this.amount.Text = userTrade.Volume.ToString();

            // Set the order type text and color
            if (userTrade.DealType == "ofr")
            {
                sellOrbuy.Text = "卖出";
            }
            else if (userTrade.DealType == "bid")
            {
                sellOrbuy.Text = "买入";
            }
            confirmButton.Focus();
        }

        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            IsConfirmed = true;
            DialogResult = true;
            //Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            IsConfirmed = false;
            Close();
        }

        private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 检查哪个按钮当前具有焦点并触发相应的点击事件
                if (confirmButton.IsFocused)
                {
                    ConfirmButton_Click(sender, e);
                    e.Handled = true; // 标记事件已处理
                }
                else if (cancelButton.IsFocused)
                {
                    //DialogResult = false;
                    IsConfirmed = false;
                    Close();
                    e.Handled = true; // 标记事件已处理
                }
            }
            if (e.Key == Key.Escape)
            {
                DialogResult = false;
                Close();
                e.Handled = true; // 标记事件已处理
            }
            if (e.Key == Key.Left)
            {
                // 设置焦点到确认按钮
                confirmButton.Focus();
                e.Handled = true; // 标记事件已处理
            }
            else if (e.Key == Key.Right)
            {
                // 设置焦点到取消按钮
                cancelButton.Focus();
                e.Handled = true; // 标记事件已处理
            }
        }

        private void Grid_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
         
        }
    }
}
